﻿package org.grizzly.web.interceptor;

import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.common.ApplicationConstant;
import org.grizzly.entity.LoginInfo;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import flex.lms.entity.User;


public class UserPermissionInterceptor extends AbstractInterceptor {

	
	private static final long serialVersionUID = -2185920708747626659L;
	
	private static final Log logger = LogFactory.getLog(UserPermissionInterceptor.class);

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ac = invocation.getInvocationContext();
		
		ServletContext servletContext = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
		WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
		
		String actionName = ac.getName();
		String methodName = "";
		Map map = ac.getParameters();
		String[] _methodName = (String[]) map.get("method");
		if (methodName != null) {
			methodName = _methodName[0];
		}
		
		logger.info("Interceptor UserPermissionInterceptor ... ");

		if (wc == null) {
			logger.error("ApplicationContext could not be found.");
			return "error";
		}
		else
		{
			//判断sesion是否失效
			User user= (User) ac.getSession().get(ApplicationConstant.LOGININFO_KEY);
			if(user == null)
			{
				logger.error("用户session失效！");
				return "sessionError";
			}
			
			/*if("__buffaloCall".equals(methodName))//buffalo ajax 不需要进行验证
			{
				return invocation.invoke();
			}*/
		}
		return invocation.invoke();
	}

}
